package com.tencent.cloud.iov.util.backdoor.authority;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

import com.tencent.cloud.iov.util.backdoor.DebugBackdoor;

/**
 * 域名切换入口
 *
 * @author showxiao
 * @date 2018/10/11
 */
public class AuthoritySelectorListener implements View.OnLongClickListener {

    private PopupWindow mPopupWindow;

    @Override
    public boolean onLongClick(View v) {
        final Context context = v.getContext();

        RadioGroup radioGroup = new RadioGroup(context);
        radioGroup.setOrientation(LinearLayout.VERTICAL);
        radioGroup.setGravity(Gravity.CENTER);

        AuthorityEnum authority = DebugBackdoor.getAuthority();

        for (final AuthorityEnum authorityEnum : AuthorityEnum.values()) {
            RadioButton radioButton = new RadioButton(context);
            radioButton.setText(authorityEnum.name);
            radioButton.setButtonDrawable(null);
            radioButton.setTextColor(
                    (authorityEnum == authority
                            || (authority == null && DebugBackdoor.DEFAULT_AUTHORITY == authorityEnum)
                    )
                    ? Color.BLUE : Color.BLACK
            );

            radioButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    DebugBackdoor.setAuthority(authorityEnum);
                    mPopupWindow.dismiss();
                    Toast.makeText(context, "切换至" + authorityEnum.name, Toast.LENGTH_SHORT)
                            .show();
                }
            });
            radioGroup.addView(radioButton);
        }

        mPopupWindow = new PopupWindow(radioGroup,
                ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.WRAP_CONTENT);
        mPopupWindow.setBackgroundDrawable(new ColorDrawable(Color.WHITE));
        mPopupWindow.setOutsideTouchable(true);
        mPopupWindow.showAsDropDown(v);

        return true;
    }
}
